<?php
class Page extends CActiveRecord
{
    
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    public function tableName()
    {
        return 'page';
    }

    public function rules()
    {

        return array(
                    array('title, content, description, url, lang_id', 'required'),
                    array('lang_id, parent_id', 'numerical', 'integerOnly'=>true),
                    array('title', 'length', 'max'=>255),
                    array('url', 'length', 'max'=>100),
                    array('m_d, m_k', 'safe'),
                    array('id, title, parent_id, m_d, m_k, content, description, url, lang_id', 'safe', 'on'=>'search'),
            );
    }

    public function relations()
    {
        return array(
                    'lang' => array(self::BELONGS_TO, 'Language', 'lang_id'),
            );
    }

    public function attributeLabels()
    {
        return array(
                'id'            => 'ID',
                'title'         => Yii::t('system','Заглавие'),
                'm_d'           => Yii::t('system','Описание'),
                'm_k'           => Yii::t('system','Ключевые слова'),
                'content'       => Yii::t('page','Контент'),
                'description'   => Yii::t('system','Краткое описание'),
                'url'           => Yii::t('page','ЧПУ'),
                'lang_id'       => Yii::t('language','Язык'),
        );
    }
    
    public static function getPageTranslations($id){
        $command=Yii::app()->db->createCommand()
                ->select('p.id as page_id, p.title, l.name, l.id as language_id')
                ->from('Page p')
                ->join('Language l', 'p.lang_id = l.id')
                ->where('p.parent_id = :id',array(':id'=>$id))
                ->queryAll();
        
        $count_language = Language::countLang()-1;

        $language_for_item = Array();

        foreach ($command as $key => $value) {
            $language_for_item[] = $value['language_id'];
            $view .= CHtml::link($value['name'].' - '.$value['title'],array('/page/admin/update','id'=>$value['page_id'])).'<br>';
        }

        if ($count_language > count($command)){
            foreach (Language::allExcept($language_for_item) as $value) {
                $view .= CHtml::link($value['name'].' - Нет перевода',array('/page/admin/create','language_id'=>$value['id'],'parent_id'=>$id),array('class'=>'warning')).'<br>';
            }
        }
        return $view;
    }

    public function search()
    {
        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('title',$this->title,true);
        $criteria->compare('m_d',$this->m_d,true);
        $criteria->compare('m_k',$this->m_k,true);
        $criteria->compare('content',$this->content,true);
        $criteria->compare('description',$this->description,true);
        $criteria->compare('url',$this->url,true);
        $criteria->compare('lang_id',Language::getIdForLang(Yii::app()->params['dafaultLanguage']));
        return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
        ));
    }
}